﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WxYygh.Common;

namespace WxYygh.Controllers
{
    public class WeChatBase : Controller
    {
        private WeChatBase() { }

        static string CORPID;
        static string SECRET;

        static string qyhCORPID;
        static string qyhSECRET;

        /// <summary>
        /// .Ctor
        /// </summary>
        static WeChatBase()
        {
            CORPID = ConfigurationManager.AppSettings["appID"];
            SECRET = ConfigurationManager.AppSettings["AppSecret"];

            qyhCORPID = ConfigurationManager.AppSettings["CorpID"];
            qyhSECRET = ConfigurationManager.AppSettings["SECRET"];

        }

        #region 微信企业号配置信息

        public static string GetCorpId()
        {
            return CORPID;
        }



        #endregion

        #region GetAccessToken：获取微信访问凭证

        /// <summary>
        /// ACCESS_TOKEN最后一次更新时间
        /// </summary>
        static DateTime _lastGetTimeOfAccessToken = DateTime.Now.AddSeconds(-7201);

        /// <summary>
        /// 微信访问凭证
        /// </summary>
        static string _AccessToken;

        /// <summary>
        /// 微信访问凭证
        /// </summary>
        public static string GetAccessToken()
        {
            try
            {
                if (_lastGetTimeOfAccessToken < DateTime.Now)
                {

                    // 每隔7200秒刷新一次 ACCESS_TOKEN
                    string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", CORPID, SECRET);
                    string responseText = HttpHelper.Instance.get(url);

                    var rsEntity = new { access_token = "", expires_in = 0, errcode = 0, errmsg = "" };
                    dynamic en = JsonHelper.GetEntity<object>(responseText, rsEntity);
                    _lastGetTimeOfAccessToken = DateTime.Now.AddSeconds((double)en.expires_in - 1);
                    _AccessToken = en.access_token;

                    LogHelper.WriteLog("_AccessToken=" + _AccessToken);
                }
                return _AccessToken;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 微信访问凭证
        /// </summary>
        public static string GetQYHAccessToken()
        {
            try
            {
                if (_lastGetTimeOfAccessToken < DateTime.Now)
                {

                    // 每隔7200秒刷新一次 ACCESS_TOKEN
                    string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", qyhCORPID, qyhSECRET);
                    string responseText = HttpHelper.Instance.get(url);

                    var rsEntity = new { access_token = "", expires_in = 0, errcode = 0, errmsg = "" };
                    dynamic en = JsonHelper.GetEntity<object>(responseText, rsEntity);
                    _lastGetTimeOfAccessToken = DateTime.Now.AddSeconds((double)en.expires_in - 1);
                    _AccessToken = en.access_token;
                }
                return _AccessToken;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region GetJSapiTicket：获取JSSDK的访问凭证

        /// <summary>
        /// JSAPI_TICKET最后一次更新时间
        /// </summary>
        static DateTime LastGetTimeOfJSapiTicket = DateTime.Now.AddSeconds(-7201);

        /// <summary>
        /// JSAPI的访问凭证
        /// </summary>
        static string _jsapiTicket;

        /// <summary>
        /// 微信访问凭证
        /// </summary>
        public static string GetJSapiTicket()
        {
            try
            {
                if (LastGetTimeOfJSapiTicket < DateTime.Now)
                {
                    // 每隔7200秒刷新一次 JSAPI_TICKET
                    string url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", GetAccessToken());
                    string responseText = HttpHelper.Instance.get(url);

                    var rsEntity = new { ticket = "", expires_in = 0, errcode = 0, errmsg = "" };
                    dynamic en = JsonHelper.GetEntity<object>(responseText, rsEntity);
                    _lastGetTimeOfAccessToken = DateTime.Now.AddSeconds((double)en.expires_in - 1);
                    _jsapiTicket = en.ticket;
                }
                return _jsapiTicket;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

       
      
    }
}